package models

import (
	"gorm.io/gorm"
	"order_srv/basic/config"
	"time"
)

// 收费信息
type PricingInformation struct {
	Id              uint64         `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	OrderId         uint64         `gorm:"column:order_id;type:bigint UNSIGNED;comment:订单id;not null;" json:"order_id"`                              // 订单id
	PricesStarting  float64        `gorm:"column:prices_starting;type:decimal(10, 2);comment:起步价(2公里/10元);not null;" json:"prices_starting"`         // 起步价(2公里/10元)
	MileageFee      float64        `gorm:"column:mileage_fee;type:decimal(10, 2);comment:里程费(超出的每公里3元);not null;" json:"mileage_fee"`                // 里程费(超出的每公里3元)
	DurationFee     float64        `gorm:"column:duration_fee;type:decimal(10, 2);comment:时长费(每分钟0.5元);not null;" json:"duration_fee"`               // 时长费(每分钟0.5元)
	LongDistance    float64        `gorm:"column:long_distance;type:decimal(10, 2);comment:远途费;default:0.00;" json:"long_distance"`                  // 远途费
	Bridge          float64        `gorm:"column:bridge;type:decimal(10, 2);comment:过路过桥费;default:0.00;" json:"bridge"`                              // 过路过桥费
	NightServiceFee float64        `gorm:"column:night_service_fee;type:decimal(10, 2);comment:夜间服务费;default:0.00;" json:"night_service_fee"`        // 夜间服务费
	ParkingFee      float64        `gorm:"column:parking_fee;type:decimal(10, 2);comment:停车费;default:0.00;" json:"parking_fee"`                      // 停车费
	Other           float64        `gorm:"column:other;type:decimal(10, 2);comment:其他费用;default:0.00;" json:"other"`                                 // 其他费用
	TotalPrice      float64        `gorm:"column:total_price;type:decimal(10, 2);comment:总价格;not null;" json:"total_price"`                          // 总价格
	CreatedAt       time.Time      `gorm:"column:created_at;type:datetime(3);comment:创建时间;not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"` // 创建时间
	UpdatedAt       time.Time      `gorm:"column:updated_at;type:datetime(3);comment:修改时间;not null;default:CURRENT_TIMESTAMP(3);" json:"updated_at"` // 修改时间
	DeletedAt       gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);comment:删除时间;default:NULL;" json:"deleted_at"`                          // 删除时间
}

func (p *PricingInformation) TableName() string {
	return "pricing_information"
}

// 添加收费信息
func (p *PricingInformation) PricingInformationAdd() error {
	return config.DB.Create(p).Error
}

// 订单id查询
func (p *PricingInformation) PricingInformationOrderId(orderId uint64) error {
	return config.DB.Where("order_id = ?", orderId).Limit(1).Find(p).Error
}

// 更新收费信息
func (p *PricingInformation) PricingInformationUpdate() error {
	return config.DB.Model(p).Updates(map[string]interface{}{
		"prices_starting":  p.PricesStarting,
		"mileage_fee":      p.MileageFee,
		"duration_fee":     p.DurationFee,
		"long_distance":    p.LongDistance,
		"bridge":           p.Bridge,
		"night_service_fee": p.NightServiceFee,
		"parking_fee":      p.ParkingFee,
		"other":            p.Other,
		"total_price":      p.TotalPrice,
		"updated_at":       time.Now(),
	}).Error
}
